<?php

class SessionController extends MyController
{	
	function init() {parent::init(); }
	
	function actionIndex()
	{	
		$this->redirect(array($this->controller_id.'/view'));
	}
	
	function actionView($page = 1)
	{
		$pages = new CPagination;
		$pages->pageSize = $this->website["beperpage"];

		$rows = NULL;

		$rows = session::model()->findAll(array(
			"order" => "`time_online` DESC",
			"offset" => ($page - 1) * $this->website["beperpage"],
			"limit" => $this->website["beperpage"]
		));
		
		// tong so dong
		$pages->itemCount =  session::model()->count();
			
		$this->data['rows']= $rows;		
		
		$this->data['pages']= $pages;	
		$this->data['page']= $page;	
		$this->render('view', $this->data);
	}
	
	function actionEdit($session_id = 0, $page = 1)
	{
		$row = NULL;
		
		$row = session::model()->findByPk($session_id);
		
		$this->data["row"] = $row;
		$this->data["page"] = $page;
		
		$this->action = "Cập nhật";
		$this->render("form_edit", $this->data);
	}
	
	function actionSave($session_id = 0, $page = 0)
	{
		$row = session::model()->findByPk($session_id);
		$row->setAttributes($_POST, false);
		if(strlen($row->id))
			$row->save();
		
		$this->redirect(array($this->controller_id."/view", "page"=>$page));
	}
	
	function actionDelete($session_id, $page= 1)
	{
		session::model()->deleteByPk($session_id);
		
		$rows = session::model()->findAll(array(
			"offset" => ($page - 1) * $this->website["beperpage"],
			"limit" => $this->website["beperpage"]
		));
		
		if (count($rows) == 0)
		{
			if($page > 1)
				$page--;
		}
		
		$this->redirect(array($this->controller_id.'/view',"page"=>$page));
	}
	
	
	function actionDelete_multi($page = 1)
	{
		//var_dump(Yii::app()->getRequest()->getPost("cib"));
		
		$id_array = Yii::app()->getRequest()->getPost("cib");
		
		if (!is_null($id_array))
		{			
			foreach($id_array as $id=>$value)
			{
				session::model()->deleteByPk($id);
			}
		}
		
		$rows = session::model()->findAll(array(
			"offset" => ($page - 1) * $this->website["beperpage"],
			"limit" => $this->website["beperpage"]
		));
		
		if (count($rows) == 0)
		{
			if($page > 1)
				$page--;
		}
		
		$this->redirect(array($this->controller_id.'/view',  "page"=>$page));
	}
	
}